var manageFileCtrl = myApp.controller('manageFileCtrl', ['$scope', '$rootScope',  function($scope, $rootScope) {
	$rootScope.firstType = Tools.permissionList[4].permissionId;
	$rootScope.secondType = Tools.permissionList[4].sonPermission[0].permissionId;
	
	$scope.modItem = {};

	$scope.msg = "";
	$scope.visible = false;

	// 获取园所档案表单信息
	$scope.getSchoolForm = function() {
		commonSrv.getSchoolForm({}, function(result) {
			if (result.status == 0) {
				$scope.modItem = result.data.school;
				// 园所图标
				$scope.modItem.logo = Tools.parseItemToArray($scope.modItem.logo);
				// 微信家长端首页图标
				$scope.modItem.weiParentLogo = Tools.parseItemToArray($scope.modItem.weiParentLogo);
				// 微信教师端首页图标
				$scope.modItem.weiTeacherLogo = Tools.parseItemToArray($scope.modItem.weiTeacherLogo);

				// 建园日期
				var beginDate = moment($scope.modItem.beginDate).toDate();
				$("#beginDate").datepicker({
					format: "yyyy-mm-dd",
					language: 'zh-CN'
				});
				$("#beginDate").datepicker("setDate", beginDate);

				$scope.modItem.select = {
					province: $scope.modItem.province,
					city: $scope.modItem.city,
					district: $scope.modItem.district
				};
				$scope.modItem.warn = {
					name: ""
				};
				$.ajax({
    				// url: '/ta/CityCtrl/getAllCity.do',
    				url: '../common/libs/allCity.json',
    				data: {},
    				success: function(result) {
    					if (typeof result == 'string') {
    						result = JSON.parse(result);
    					}
    					// 省市区
    					$scope.allCity = result;
						// 省
						var provinceList = [];
    					for(var i in $scope.allCity) {
    						provinceList.push(i);
    					}
    					var province = $scope.modItem.province;
    					$scope.modItem.provinceList = provinceList;
    					for(var i in $scope.modItem.provinceList) {
    						if(province == $scope.modItem.provinceList[i]) {
    							province = $scope.modItem.provinceList[i];
    							break;
    						}
    					}
    					// 市
						var cityListTemp = [];
						if(province && province != '') {
							cityListTemp = angular.copy($scope.allCity[province]);
						}
						var cityList = [];
						// 这里的cityListTemp是对象不是数组，注意！！！
						if(cityListTemp && !(cityListTemp instanceof Array)) {
							for(var i in cityListTemp) {
								cityList.push(i);
							}
						}
						var city = $scope.modItem.city;
						$scope.modItem.cityList = cityList;
						if(cityList && cityList.length > 0) {
							for(var i in $scope.modItem.cityList) {
	    						if(city == $scope.modItem.cityList[i]) {
	    							city = $scope.modItem.cityList[i];
	    							break;
	    						}
	    					}
    					}
						
						// 区
		    			var districtList = [];
		    			if(city && city != '') {
		    				districtList = angular.copy($scope.allCity[province][city]);
		    			}
						var district = $scope.modItem.district;
						$scope.modItem.districtList = districtList;
						if(districtList && districtList.length >0) {
							for(var i in $scope.modItem.districtList) {
	    						if(district == $scope.modItem.districtList[i]) {
	    							district = $scope.modItem.districtList[i];
	    							break;
	    						}
	    					}
						}

    				}, fail: function() {
    					alert("获取地区列表失败");
    				}, complete: function() {
    					$scope.$apply();
    				}
    			});
			}
		}, function(result) {
			if(result.status == 1) {
				alert(result.msg);
			}
		}, function() {
			$scope.$apply();
		})
	};

	// 提交
	$scope.editSchool = function() {
		// 不能为空
		$scope.watchName('modItem');

		if(!Tools.reg.isNull($scope.modItem.warn.name)) {
			alert("请修改表单")
			return;
		}

		// 名称
    	var name = "";
    	if($scope.modItem.name == null || $scope.modItem.name == undefined) {
    		name = "";
    	} else {
			name = $scope.modItem.name;
    	}
    	// 园所编码
    	var code = "";
    	if($scope.modItem.code == null || $scope.modItem.code == undefined) {
    		code = "";
    	} else {
			code = $scope.modItem.code;
    	}
    	// 园所简称
    	var simpleName = "";
    	if($scope.modItem.simpleName == null || $scope.modItem.simpleName == undefined) {
    		simpleName = "";
    	} else {
			simpleName = $scope.modItem.simpleName;
    	}
		// 园所图标
    	var logo = "";
    	if($scope.modItem.logo == null || $scope.modItem.logo == undefined) {
    		logo = "";
    	} else {
			logo = $scope.modItem.logo;
    	}
		// 微信家长端首页图标
    	var weiTeacherLogo = "";
    	if($scope.modItem.weiTeacherLogo == null || $scope.modItem.weiTeacherLogo == undefined) {
    		weiTeacherLogo = "";
    	} else {
			weiTeacherLogo = $scope.modItem.weiTeacherLogo;
    	}
    	// 微信教师端首页图标
    	var weiParentLogo = "";
    	if($scope.modItem.weiParentLogo == null || $scope.modItem.weiParentLogo == undefined) {
    		weiParentLogo = "";
    	} else {
			weiParentLogo = $scope.modItem.weiParentLogo;
    	}

    	// 地址
    	var address = "";
    	if($scope.modItem.address == null || $scope.modItem.address == undefined) {
    		address = "";
    	} else {
			address = $scope.modItem.address;
    	}
    	// 邮编
    	var zipCode = "";
    	if($scope.modItem.zipCode == null || $scope.modItem.zipCode == undefined) {
    		zipCode = "";
    	} else {
			zipCode = $scope.modItem.zipCode;
    	}
    	// 建园日期
    	var beginDate = "";
    	if($scope.modItem.beginDate == null || $scope.modItem.beginDate == undefined) {
    		beginDate = "";
    	} else {
			beginDate = $scope.modItem.beginDate;
    	}
    	// 园长姓名
    	var principal = "";
    	if($scope.modItem.principal == null || $scope.modItem.principal == undefined) {
    		principal = "";
    	} else {
			principal = $scope.modItem.principal;
    	}
    	// 联系电话
    	var tel = "";
    	if($scope.modItem.tel == null || $scope.modItem.tel == undefined) {
    		tel = "";
    	} else {
			tel = $scope.modItem.tel;
    	}
    	// 邮件地址
    	var email = "";
    	if($scope.modItem.email == null || $scope.modItem.email == undefined) {
    		email = "";
    	} else {
			email = $scope.modItem.email;
    	}
    	// 园所网站
    	var site = "";
    	if($scope.modItem.site == null || $scope.modItem.site == undefined) {
    		site = "";
    	} else {
			site = $scope.modItem.site;
    	}

    	// 总面积
    	var totalArea = 0;
    	if($scope.modItem.totalArea == null || $scope.modItem.totalArea == undefined) {
    		totalArea = 0;
    	} else {
			totalArea = $scope.modItem.totalArea;
    	}
    	// 室内面积
    	var innerArea = 0;
    	if($scope.modItem.innerArea == null || $scope.modItem.innerArea == undefined) {
    		innerArea = 0;
    	} else {
			innerArea = $scope.modItem.innerArea;
    	}
    	// 室外面积
    	var outerArea = 0;
    	if($scope.modItem.outerArea == null || $scope.modItem.outerArea == undefined) {
    		outerArea = 0;
    	} else {
			outerArea = $scope.modItem.outerArea;
    	}
    	// 最大班级数
    	var maxClassCount = 0;
    	if($scope.modItem.maxClassCount == null || $scope.modItem.maxClassCount == undefined) {
    		maxClassCount = 0;
    	} else {
			maxClassCount = $scope.modItem.maxClassCount;
    	}
    	// 最大学生数
		var maxStudentCount = 0;
    	if($scope.modItem.maxStudentCount == null || $scope.modItem.maxStudentCount == undefined) {
    		maxStudentCount = 0;
    	} else {
			maxStudentCount = $scope.modItem.maxStudentCount;
    	}

		// 省
    	if(typeof $scope.modItem.select.province != "string") {
    		$scope.modItem.select.province = "";
    	}
    	// 市
    	if(typeof $scope.modItem.select.city != "string") {
    		$scope.modItem.select.city = "";
    	}
    	// 区
    	if(typeof $scope.modItem.select.district != "string") {
    		$scope.modItem.select.district = "";
    	}

    	// 判断有无logo
    	$scope.upload(logo, function(picName) {
    		// 判断有无weiParentLogo
    		logo = picName;
    		$scope.upload(weiParentLogo, function(picName) {
    			weiParentLogo = picName;
    			// 判断有无weiTeacherLogo
    			$scope.upload(weiTeacherLogo, function(picName) {
    				weiTeacherLogo = picName;
					var data = {
						address: address,
						beginDate: beginDate,
						code: code,
						email: email,
						innerArea: innerArea,
						logo: logo,
						maxClassCount: maxClassCount,
						maxStudentCount: maxStudentCount,
						name: name,
						outerArea: outerArea,
						principal: principal,
						simpleName: simpleName,
						site: site,
						tel: tel,
						totalArea: totalArea,
						weiParentLogo: weiParentLogo,
						weiTeacherLogo: weiTeacherLogo,
						zipCode: zipCode,
						province: $scope.modItem.select.province,
						city: $scope.modItem.select.city,
						district: $scope.modItem.select.district
					};
					// 提交表单
					commonSrv.editSchool(data, function(result) {
						if (result.status == 0) {
							$scope.msg = "修改成功";
							$scope.visible = true;
						}
					}, function(result) {
						if(result.status == 1) {
							alert(result.msg);
						}
					}, function() {
						$scope.$apply();
					});
    			});
    		});
    	});
	};

	// 取消==重置
	$scope.reset = function() {
		$scope.getSchoolForm();
	}

	// 上传logo
    $scope.uploadLogo = function(key, file) {
    	if (!file || file.length == 0) {
            return;
        }
        var uploadFiles;
        if (angular.isArray(file)) {
            uploadFiles = file;
        } else {
            uploadFiles = [file];
        }
        $scope.modItem[key] = uploadFiles;
    };
    // 删除logo
    $scope.delete = function(key) {
    	$scope.modItem[key] = [];
    };
    // 下载logo
    $scope.download = function(file) {
    	console.log(file);
    };

    /**
     * 上传logo至服务器,先判断数组是否有内容
     * @param  {[Array]}   logo [图片数组]
     * @param  {Function} cb   [回调函数]
     */
    $scope.upload = function(logo, cb) {
    	var picName = "";
    	if(logo.length > 0) {
    		if(typeof logo[0] == 'string') {
    			picName = logo[0];
    			cb(picName);
    		} else {
		    	var formData = new FormData();
				formData.append('file', logo[0]);
				$.ajax({
					url: Tools.config.domain + '/ta/UploadCtrl/uploadPic.do',
					data: formData,
					type: 'POST',
		    		cache: false,
				    processData: false,
				    contentType: false,
					success: function(result) {
						if(typeof result == 'string') {
							result = JSON.parse(result);
						}
						if(result.status == 0) {
							picName = result.data;
				    		if(typeof cb == 'function') {
					    		cb(picName);
				    		}
						}
					},
					fail: function(result) {
						if(result.status == 3 || result.status == 1) {
							alert(result.msg);
						}
					},
					complete: function() {
						$scope.$apply();
					}
				});
			}
		} else {
			cb(picName);
		}
    }

    // 打开日期插件
	$scope.openDate = function(target) {
		var $myDatePicker = $(target).parents('.input-group').children('.myDatePicker');
		$myDatePicker.datepicker('show');
	};
	// 清除日期值
	$scope.cleanDate = function(target) {
		var $myDatePicker = $(target).parents('.input-group').children('.myDatePicker');
		$myDatePicker.datepicker('update', '');
	};
    // 选择省份
	$scope.selectProvince = function() {
		var temp = [];
		temp = angular.copy($scope.allCity[$scope.modItem.select.province]);
		
		var cityList = [];
		for(var i in temp) {
			cityList.push(i);
		}

		$scope.modItem.cityList = cityList;
		$scope.modItem.select.city = "";
		$scope.modItem.districtList = [];
		$scope.modItem.select.district = "";
	};
	// 选择城市
	$scope.selectCity = function() {
		var temp = [];
		temp = angular.copy($scope.allCity[$scope.modItem.select.province][$scope.modItem.select.city]);
		var districtList = [];
		for(var i in temp) {
			districtList.push(temp[i]);
		}
		$scope.modItem.districtList = districtList;
		$scope.modItem.select.district = "";
	};
	// 选择区域
    $scope.selectDistrict = function() {

	};
	
	// 公用方法
	$scope.isString = function(input) {
		return Tools.isString(input);
	};

	// 验证
    // 1.园所名称
	$scope.watchName = function (type) {
		var nullWarn = "姓名不能为空";
		if(Tools.reg.isNull($scope[type].name)) {
			$scope[type].warn.name = nullWarn;
		} else {
			$scope[type].warn.name = "";
		}
	};

	// 初始化
	function init() {
		$scope.getSchoolForm();
		$(document).ready(function() {
			// console.log($rootScope.secondType);
		});
	}
	init();

}]);